﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class SFTJ : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialDropDown();
            }
        }

        private void InitialDropDown()
        {
            for (int i = 2010; i <= DateTime.Now.Year; i++)
            {
                yearDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            for (int i = 1; i <= 12; i++)
            {
                monthDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            ServiceStationService service = new ServiceStationService();
            serviceStationDropDownList.DataSource = service.GetALLServiceStations();
            serviceStationDropDownList.DataTextField = "Name";
            serviceStationDropDownList.DataValueField = "ID";
            serviceStationDropDownList.DataBind();
            yearLabel.Text = yearDropDownList.SelectedValue;
            monthLabel.Text = monthDropDownList.SelectedValue;
            servicestationLabel.Text = serviceStationDropDownList.SelectedItem.Text;
        }

        protected void searchButton_Click(object sender, EventArgs e)
        {
            yearLabel.Text = yearDropDownList.SelectedValue;
            monthLabel.Text = monthDropDownList.SelectedValue;
            servicestationLabel.Text = serviceStationDropDownList.SelectedItem.Text;
            BindProduct();
            GetGathering();
        }

        protected void viewLinkButton_Click(object sender, EventArgs e)
        {
            hfProductID.Value = ((LinkButton)sender).CommandArgument;
            maintainRecordModalPopupExtender.Show();
            BindMaintainRecord(long.Parse(hfProductID.Value));
        }

        #region maintain record

        private void BindMaintainRecord(long p)
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(p);
            if (!product.ApplicationFormReference.IsLoaded)
            {
                product.ApplicationFormReference.Load();
            }
            if (!product.ProductTypeReference.IsLoaded)
            {
                product.ProductTypeReference.Load();
            }
            if (!product.QuatationForm.IsLoaded)
            {
                product.QuatationForm.Load();
            }
            if (!product.ProblemAnalyzeReference.IsLoaded)
            {
                product.ProblemAnalyzeReference.Load();
            }
            rmaMaintainLabel.Text = product.ApplicationForm.ApplicationCode;
            typeLabel.Text = product.ProductType.ProductTypeName;
            serialLabel.Text = product.ProductSerialNumber;
            wxwcrqLabel.Text = product.RepairCompleteDateTime.Value.ToString("yyyy-MM-dd");
            blxxTextBox.Text = product.ProductProblemDescription;
            blyyDropDownList.SelectedValue = product.ReasonToProblem;
            blljTextBox.Text = product.RepairAccessory;
            ljwzTextBox.Text = product.AccessoryPlace;
            if (product.ChangeType != null)
            {
                if (product.ChangeType.EndsWith("更换"))
                {
                    ghRadioButton.Checked = true;
                }
                else if (product.ChangeType.EndsWith("替换"))
                {
                    thRadioButton.Checked = true;
                }
                thSerialNumberTextBox.Text = product.ReplaceProductSerialNumber;
            }
            double totalCost = 0;
            foreach (QuatationForm quatation in product.QuatationForm)
            {
                totalCost += quatation.Cost;
            }
            bjLabel.Text = totalCost.ToString();
            bzTextBox.Text = product.Remark;
            BindAnalyze(product.ProblemAnalyze);
        }

        private void BindAnalyze(ProblemAnalyze problemAnalyze)
        {
            if (problemAnalyze != null)
            {
                if (!string.IsNullOrEmpty(problemAnalyze.A))
                {
                    switch (problemAnalyze.A)
                    {
                        case "有缺陷":
                            RadioButton1.Checked = true;
                            break;
                        case "正常损坏":
                            RadioButton2.Checked = true;
                            break;
                        case "使用不当":
                            RadioButton3.Checked = true;
                            break;
                        case "非人为(不可抗力等)":
                            RadioButton4.Checked = true;
                            break;
                    }
                }

                if (!string.IsNullOrEmpty(problemAnalyze.B))
                {
                    switch (problemAnalyze.B)
                    {
                        case "环境不适":
                            RadioButton5.Checked = true;
                            break;
                        case "操作不当":
                            RadioButton6.Checked = true;
                            break;
                        case "正常损坏":
                            RadioButton7.Checked = true;
                            break;
                    }
                }

                if (!string.IsNullOrEmpty(problemAnalyze.C))
                {
                    switch (problemAnalyze.C)
                    {
                        case "固件Bug":
                            RadioButton8.Checked = true;
                            break;
                        case "正常升级":
                            RadioButton9.Checked = true;
                            break;
                    }
                }

                if (!string.IsNullOrEmpty(problemAnalyze.D))
                {
                    if (problemAnalyze.D.Equals("RestestOK"))
                    {
                        RadioButton10.Checked = true;
                    }
                }

                if (!string.IsNullOrEmpty(problemAnalyze.E))
                {
                    if (problemAnalyze.E.Equals("其它原因"))
                    {
                        RadioButton11.Checked = true;
                    }
                }
            }
        }
        #endregion

        private void GetGathering()
        {
            GatheringService service = new GatheringService();
            IQueryable<WXYW.DAL.Gathering> gatherings = service.GetGatheringsByYearMonthService(Convert.ToInt32(yearDropDownList.SelectedValue), Convert.ToInt32(monthDropDownList.SelectedValue), Convert.ToInt32(serviceStationDropDownList.SelectedValue));
            realLabel.Text = gatherings.Sum(g => g.RealPay).ToString();
            resultLabel.Text = realLabel.Text;
        }

        private void BindProduct()
        {
            ApplicationProductService service = new ApplicationProductService();
            IQueryable<ApplicationProduct> products = service.GetProductByYearMonthService(Convert.ToInt32(yearDropDownList.SelectedValue), Convert.ToInt32(monthDropDownList.SelectedValue), Convert.ToInt32(serviceStationDropDownList.SelectedValue));
            foreach (ApplicationProduct product in products)
            {
                if (!product.ApplicationFormReference.IsLoaded)
                {
                    product.ApplicationFormReference.Load();
                }
                if (!product.ProductTypeReference.IsLoaded)
                {
                    product.ProductTypeReference.Load();
                }
            }

            productGridView.DataSource = products;
            productGridView.DataBind();

            totalLabel.Text = products.Sum(p => p.Cost).ToString();
            shouldLabel.Text = totalLabel.Text;
        }
    }
}
